package utils

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego"
)

func init(){
	/*DB_username := beego.AppConfig.String("configdb::db_username")
	DB_password := beego.AppConfig.String("configdb::db_password")
	DB_name := beego.AppConfig.String("configdb::db_name")
	DB_ip := beego.AppConfig.String("configdb::db_ip")
	DB_port := beego.AppConfig.String("configdb::db_port")

	conn := DB_username + ":" + DB_password +"@tcp("+DB_ip+":"+DB_port+")/"+ DB_name + "?charset=utf8"//组合成连接串
	orm.RegisterDriver("mysql", orm.DRMySQL) //注册mysql驱动
	orm.RegisterDataBase("default", "mysql", conn) //设置conn中的数据库为默认使用数据库

	//注册模型
	orm.RegisterModel(new(TblDockerTemplate))
	//orm.RegisterModel(new(AppService))
	//orm.RegisterModel(new(BambooService))

	orm.RunSyncdb("default", false, true)//后一个使用true会带上很多打印信息，数据库操作和建表操作的；第二个为true代表强制创建表*/
}

func SetJsonData(o orm.Ormer,tableName string, id int64, json []byte) {
	p, err := o.Raw("UPDATE "+tableName+" SET json = ? WHERE id = ?").Prepare()
	CheckErr(err)
	_,err = p.Exec(json,id)
	CheckErr(err)
	p.Close()
}

func GetJsonById(o orm.Ormer,tableName string, id int64) string{
	json := []byte{}

	db,err := orm.GetDB()
	rows,err := db.Query("SELECT json FROM "+tableName+" WHERE id = ?", id)
	defer rows.Close()

	CheckErr(err)

	for ;rows.Next();{
		rows.Scan(&json)
	}

	beego.Info(string(json))
	return string(json)
}

/*
type JsonStruct struct{
	Id int
	Json []byte
}*/
